import Vue from "vue";
import { getdownLoadUrl } from '../../config/api/index';
// import VConsole from 'vconsole';

// const vConsole = new VConsole();
// import viewportUnitsBuggyfill from "viewport-units-buggyfill"
// viewportUnitsBuggyfill.init({
//     hacks: window.viewportUnitsBuggyfillHacks
// });
import "./index.less"
new Vue({
    el: "#app",
    data() {
        return {
            PC: false,
            wxShow: false,
            downloadMsg: {},
            shareChannel: '',
        }
    },
    created() {
        this.refresh();
        this.shareChannel = this.getUrlParam('share_channel') || 'default';
        getdownLoadUrl({
            _t: new Date().getTime(),
            app_channel: this.shareChannel
        }).then(res => {
            if (res.code == 0) {
                this.downloadMsg = res.data;
                let auto = this.getUrlParam('auto');
                if (!this.wxShow && auto == 1) {
                    this.download();
                }
            }
        })
        window.onresize = () => {  //监听屏幕的改变
            setTimeout(() => {
                this.refresh();
            }, 10)
        };
    },
    methods: {
        refresh() {
            let width = document.querySelector("html").offsetWidth;
            if (width > 1200) {
                this.PC = true;
            } else {
                this.PC = false;
                this.is_weixn();
            }
        },
        is_weixn() {
            let ua = navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == "micromessenger") {
                this.wxShow = true;
            } else {
                this.wxShow = false;
            }
        },
        download(platform) {
            // pc端
            if (this.PC) {
                switch (platform) {
                    case 'android':
                        window.location = this.downloadMsg[this.shareChannel];
                        break;
                    case 'ios':
                        window.location = "magicBox://"; //schema链接或者universal link
                        setTimeout(() => {
                            let hidden = window.document.hidden || window.document.webkitHidden || window.document
                                .mozHidden || window.document.msHidden
                            //下载app
                            if (typeof (hidden) == "undefined" || hidden == false) {
                                window.location = this.downloadMsg.ios;
                            }
                        }, 3000)
                        break;
                    default:
                        break;
                }
                return;
            }
            // 移动端
            if (this.isIOS()) {
                window.location = "magicBox://"; //schema链接或者universal link
                setTimeout(() => {
                    let hidden = window.document.hidden || window.document.webkitHidden || window.document
                        .mozHidden || window.document.msHidden
                    //下载app
                    if (typeof (hidden) == "undefined" || hidden == false) {
                        window.location = this.downloadMsg.ios;
                    }
                }, 3000)
                // window.setTimeout(function () { //如果没有安装app,便会执行setTimeout跳转下载页
                //     var timeOutDateTime = new Date();
                //     if (timeOutDateTime - loadDateTime < 5000) {
                //         window.location = this.downloadMsg.ios; //ios下载地址  
                //     } else {
                //         window.close();
                //     }
                // }, 500);
            } else if (this.isAndroid()) {
                try {
                    window.location = this.downloadMsg[this.shareChannel];
                } catch (e) {
                    console.log('e', e);
                }
            }
        },
        isIOS() {
            return Boolean(navigator.userAgent.match(/(iPhone|iPod|iPad);?/i));
        },
        isAndroid() {
            return Boolean(navigator.userAgent.match(/android/i));
        },
        getUrlParam(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURI(r[2]);
            }
            return null;
        }
    }
})
